package com.freecycle.sg.http;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.ProgressDialog;
import android.util.Log;

import com.loopj.android.http.JsonHttpResponseHandler;

public class HTTPResponseHandler extends JsonHttpResponseHandler {

	private static final String TAG = "HTTPResponseHandler";
	
	private Activity mActivity;
	private ProgressDialog dialog;

	public HTTPResponseHandler(Activity mActivity) {
		this.mActivity = mActivity;
	}
	
	public Activity getParentActivity() {
		return mActivity;
	}
	
	@Override
	public void onStart() {
		dialog = new ProgressDialog(mActivity);
		dialog.setCancelable(false);
		dialog.setIndeterminate(true);
		dialog.setMessage("Loading...");
		dialog.show();
	}
	
	@Override
	public void onSuccess(JSONArray array) {
		try {
			Log.v(TAG, array.toString(2));
		} catch (JSONException e) {
			Log.e(TAG, "Malformed returned JSON string");
		}
	}
	
	@Override
	public void onSuccess(JSONObject object) {
		try {
			Log.v(TAG, object.toString(2));
		} catch (JSONException e) {
			Log.e(TAG, "Malformed returned JSON string");
		}
	}
	
	@Override
	public void onFinish() {
		if (dialog != null && dialog.isShowing()) {
			dialog.dismiss();
		}	
	}
}
